package cn.com.dhcc.turbo.web.controller.test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.test.BaseTest;
import cn.com.dhcc.app.core.util.DateUtil;
import cn.com.dhcc.turbo.service.creatTable.CreatTableService;
import cn.com.dhcc.turbo.service.stat.StatManageService;

@Service("StructuredDataController")
public class StructuredDataController extends BaseTest {
	@Autowired
	private CreatTableService creatTableService;
	@Autowired
	StatManageService statManageService;
	@Test
	public void startTest() {
		System.out.println("Start");
		System.out.println("Creat Table");
		creatTable();
		System.out.println("Insert Data");
		insertData();
//		System.out.println("StartUpdateInsertDelete");
//		for (int i = 0; i < 5; i++) {
//			System.out.println("-----------" + i + "------------");
//			randomUpdateInsertDelete();
//		}
	}
	
//	@Test
	public void creatTable() {
		try {
			//前四类，独立表
			//第一类，没有时间戳，没有主键
//			creatTableService.testCreatTableNoKeyTS("1",5);
//			//第二类，没有时间戳，有主键
//			creatTableService.testCreatTableWithKey("2",5);
//			//第三类，有时间戳，没有主键
//			creatTableService.testCreatTableWithTS("3",5);
			//第四类，有时间戳，有主键
			creatTableService.testCreatTableWithKeyTS("4",1);
			
//			//第五类，关联表
//			//创建第五类使用的表
//			creatTableService.testCreatTableWithKeyTS("5_A",30);
//			creatTableService.testCreatTableWithKeyTS("5_B",1);
//			creatTableService.testCreatTableWithKeyTS("5_C",1);
//			creatTableService.testCreatTableWithKeyTS("5_D",1);
//			creatTableService.testCreatTableWithKeyTS("5_E",1);
//			creatTableService.testCreatTableWithKeyTS("5_F",2);
//			creatTableService.testCreatTableWithKeyTS("5_G",2);
//			creatTableService.testCreatTableWithKeyTS("5_H",2);
//			//增加第五类外键关联
//			creatTableService.testCreatTableRelation("5_A",30,"5_B","COL_7",true);
//			creatTableService.testCreatTableRelation("5_A",30,"5_C","COL_8",true);
//			creatTableService.testCreatTableRelation("5_A",30,"5_D","COL_9",true);
//			creatTableService.testCreatTableRelation("5_B",1,"5_C","COL_7",true);
//			creatTableService.testCreatTableRelation("5_B",1,"5_D","COL_8",true);
//			creatTableService.testCreatTableRelation("5_B",1,"5_E","COL_9",true);
//			creatTableService.testCreatTableRelation("5_C",1,"5_D","COL_8",false);
//			creatTableService.testCreatTableRelation("5_C",1,"5_E","COL_9",false);
//			creatTableService.testCreatTableRelation("5_D",1,"5_E","COL_9",false);
//			creatTableService.testCreatTableRelation("5_F",2,"5_E","COL_7",true);
//			creatTableService.testCreatTableRelation("5_G",2,"5_E","COL_7",false);
//			creatTableService.testCreatTableRelation("5_H",2,"5_E","COL_7",false);
		} catch (DaoException e) {
			e.printStackTrace();
		}
	}
	
//	@Test
	public void insertData() {
		try {
			//前四类，独立表插入数据
//			creatTableService.insertDataNoFK("1",5, 10);
//			creatTableService.insertDataNoFK("2",5, 10);
//			creatTableService.insertDataNoFK("3",5, 10);
			creatTableService.insertDataNoFK("4",1, 200000);
			//第五类
			//ABCDE
//			String[] fk1 = {"5_E","5_D","5_C","5_B"};
//			creatTableService.insertDataWithFK("5_A",30, 10, fk1);
//			String[] fk2 = {"5_E","5_D","5_C"};
//			creatTableService.insertDataWithFK("5_B",1, 10, fk2);
			/*String[] fk3 = {"5_E","5_D"};
			creatTableService.insertDataWithFK("5_C",1, 3, fk3);*/
//			String[] fk4 = {"5_E"};
//			creatTableService.insertDataWithFK("5_D",1, 3, fk4);
//			//FGH
//			String[] fk5 = {"5_E"};
//			creatTableService.insertDataWithFK("5_F",2, 10, fk5);
//			String[] fk6 = {"5_E","5_D","5_C"};
//			creatTableService.insertDataWithFK("5_G",2, 10, fk6);
//			String[] fk7 = {"5_E","5_D"};
//			creatTableService.insertDataWithFK("5_H",2, 10, fk7);
		} catch (DaoException e) {
			e.printStackTrace();
		}
	}
	
//	@Test
	public void updateData() {
		try {
			creatTableService.updateData("4",1,"COL_4","513","COL_5",">=5");
//			creatTableService.updateData("5_D",1,"COL_6","2333","COL_5",">=5");
//			creatTableService.updateData("5_C",1,"COL_6","3433","COL_5",">=5");
		} catch (DaoException e) {
			e.printStackTrace();
		}
	}
	
//	@Test
	public void deleteData() {
		try {
			creatTableService.deleteData("4",1,"COL_5","<=2");
		} catch (DaoException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void clearData() {
		try {
			creatTableService.deleteData("4",1,"1","=1");
//			creatTableService.deleteData("2",5,"1","=1");
//			creatTableService.deleteData("3",5,"1","=1");
//			creatTableService.deleteData("4",5,"1","=1");
//			creatTableService.deleteData("5_F",2,"1","=1");
//			creatTableService.deleteData("5_G",2,"1","=1");
//			creatTableService.deleteData("5_H",2,"1","=1");
//			creatTableService.deleteData("5_A",30,"1","=1");
//			creatTableService.deleteData("5_B",1,"1","=1");
//			creatTableService.deleteData("5_C",1,"1","=1");
//			creatTableService.deleteData("5_D",1,"1","=1");
//			creatTableService.deleteData("5_E",1,"1","=1");
		} catch (DaoException e) {
			e.printStackTrace();
		}
	}
	
//	@Test
	public void randomUpdateInsertDelete() {
//		insertData();
		String tableKind = "4";
		Integer[] tableNum = {1,2,3,4,5};
		Integer[] baseTableNum = {5};
		String[] opKind = {"Insert","Update","Delete","Sleep"};
		String condition = "=" + creatTableService.randomInt(0,9);
		try {
				for(int j = 0;j<100;j++){
				int select = creatTableService.randomInt(0, 3);
				switch (select) {
				case 0:
					System.out.println("Insert");
					insertData();
					break;
				case 1:
					System.out.println("Update");
					int selectTable = creatTableService.randomInt(0, 1);
					creatTableService.updateData(tableKind,selectTable,"COL_4",(Math.random()*1000)+"","COL_5",condition);
					break;
				case 2:
					System.out.println("Delete");
					int selectDelTable = creatTableService.randomInt(0, 5);
					creatTableService.deleteData(tableKind,selectDelTable,"COL_5",condition);
					break;
				case 3:
					System.out.println("Sleep");
					try {
						Thread.sleep(16000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
		}
		} catch (DaoException e) {
			e.printStackTrace();
		}
	}
}
